package com.dhecp.project.system.service.impl;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.utils.StringUtils;
import com.dhecp.common.utils.TypeUtils;
import com.dhecp.core.constant.DataFiltering;
import com.dhecp.core.utils.PageUtils;
import com.dhecp.framework.util.ShiroUtils;
import com.dhecp.project.system.domain.SysDictionaryMobileForField;
import com.dhecp.project.system.mapper.SysDictionaryMobileForFieldMapper;
import com.dhecp.project.system.service.SysDictionaryForFieldService;
import com.dhecp.project.system.service.SysDictionaryMobileForFieldService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 手机审核页面字段 服务层接口
 * @author DH-Xxj
 * @version 2020年9月1日
 */
@Service
public class SysDictionaryMobileForFieldServiceImpl implements SysDictionaryMobileForFieldService {

	@Autowired
	private SysDictionaryMobileForFieldMapper dictionaryMobileForFieldMapper;
	
	@Autowired
    private SysDictionaryForFieldService dictionaryForFieldService;

	/**
     * 查询手机审核页面字段
     * @param params
     * @return List-SysDictionaryMobileForField
	 * @throws Exception
	 * @author DH-Xxj
	 */
	@Override
	public List<SysDictionaryMobileForField> queryDictionaryMobileForField(Map<String, Object> params) throws Exception {
        params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));	//常规查询条件SQL语句
		PageUtils.startPage();
		return dictionaryMobileForFieldMapper.findDictionaryMobileForField(params);
	}

    /**
     * 根据表名查询手机审核页面字段
     * @param params
     * @return List-SysDictionaryMobileForField
     * @throws Exception
     * @author DH-Xxj
     */
	@Override
	public List<SysDictionaryMobileForField> queryDictionaryMobileForFieldByTableName(Map<String, Object> params) throws Exception{
		String oldModuleTable="";
		if(params.get("moduleTable")!=null) oldModuleTable=params.get("moduleTable").toString();
		params.put("moduleTable", "TS_DictionaryMobileForField");
        params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));	//常规查询条件SQL语句
		PageUtils.startPage();
		params.put("moduleTable", oldModuleTable);
		return dictionaryMobileForFieldMapper.findDictionaryMobileForFieldByTableName(params);
	}

    /**
     * 根据guid查询手机审核页面字段
     * @param guid
     * @return 实体SysDictionaryMobileForField
     * @author DH-Xxj
     */
	@Override
	public SysDictionaryMobileForField queryDictionaryMobileForFieldByGuid(String guid) {
		return dictionaryMobileForFieldMapper.findDictionaryMobileForFieldByGuid(guid);
	}

	/**
	 * 获取手机审核页面字段及各属性(以List形式返回)
	 * @param moduleTable 必要参数：功能唯一标识
	 * @param getFieldAttributesType 可选参数：查询范围（audit：[默认]简单审核页面内容，如：手机端审核）
	 * @return List-SysDictionaryMobileForField
	 * @author DH-Xxj
	 */
	@Override
	public List<SysDictionaryMobileForField> queryMobileFieldAttributesList(Map<String, Object> params) {
    	params.put("loginUser", ShiroUtils.getMandatorId()); //当前登录用户
    	if (params.get("getFieldAttributesType") == null || params.get("getFieldAttributesType") == ""){
        	params.put("getFieldAttributesType", "audit"); //audit：[默认]简单审核页面内容，如：手机端审核
    	}
    	List<SysDictionaryMobileForField> dictionaryMobileForPageFieldList = dictionaryMobileForFieldMapper.findMobileFieldAttributes(params);
		for (SysDictionaryMobileForField dictionaryMobileForField : dictionaryMobileForPageFieldList){
			if (dictionaryMobileForField.getFields() != null){
				dictionaryMobileForField.setFields(dictionaryMobileForField.getFields().toString().toLowerCase());
			}
		}
    	return dictionaryMobileForPageFieldList;
	}

	/**
	 * 获取手机审核页面字段及各属性(以Map形式返回)
	 * @param moduleTable 必要参数：功能唯一标识
	 * @param getFieldAttributesType 可选参数：查询范围（audit：[默认]简单审核页面内容，如：手机端审核）
	 * @return Map
	 * @author DH-Xxj
	 */
	@Override
	public Map<String, Object> queryMobileFieldAttributesMap(Map<String, Object> params){
    	params.put("loginUser", ShiroUtils.getMandatorId()); //当前登录用户
    	if (params.get("getFieldAttributesType") == null || params.get("getFieldAttributesType") == ""){
        	params.put("getFieldAttributesType", "audit"); //audit：[默认]简单审核页面内容，如：手机端审核
    	}
		List<SysDictionaryMobileForField> dictionaryMobileForPageFieldList = dictionaryMobileForFieldMapper.findMobileFieldAttributes(params);
		Map<String, Object> dictionaryMobileForFieldMap = new LinkedHashMap<String, Object>();
		for (SysDictionaryMobileForField dictionaryMobileForField : dictionaryMobileForPageFieldList){
			if (dictionaryMobileForField.getFields() != null){
				dictionaryMobileForField.setFields(dictionaryMobileForField.getFields().toString().toLowerCase());
			}
			dictionaryMobileForFieldMap.put(dictionaryMobileForField.getFields().toString().toLowerCase(), TypeUtils.entityToMap(dictionaryMobileForField));
		}
		
		return dictionaryMobileForFieldMap;		
	}

	/**
	 * 新增功能字段数据字典
	 * @param infoItems
	 * @return
	 * @author DH-Xxj
	 */
	@Override
	public AjaxResult insertDictionaryMobileForField(Map<String, Object> params) {
		List<SysDictionaryMobileForField> dictList = JSON.parseArray(params.get("infoItems").toString(), SysDictionaryMobileForField.class);
        if (dictList == null || dictList.size() == 0) {
            return AjaxResult.error("数据包格式有误，请检查！");
        }
        for (SysDictionaryMobileForField dictionaryMobileForField : dictList) {
        	dictionaryMobileForFieldMapper.insertDictionaryMobileForField(dictionaryMobileForField);
        }
        return AjaxResult.success("操作成功");
	}

	/**
	 * 修改功能字段数据字典
	 * @param infoItems
	 * @return
	 * @author DH-Xxj
	 */
	@Override
	public AjaxResult updateDictionaryMobileForField(Map<String, Object> params) {
		if (StringUtils.isEmpty(params)) {
			return AjaxResult.error("数据提交失败，必填参数有误！");
		}
		List<SysDictionaryMobileForField> dictList = JSON.parseArray(params.get("infoItems").toString(), SysDictionaryMobileForField.class);
		if (dictList == null || dictList.size() == 0) {
            return AjaxResult.error("数据包格式有误，请检查！");
        }
		for (SysDictionaryMobileForField dictionaryMobileForField : dictList) {
			dictionaryMobileForFieldMapper.updateDictionaryMobileForField(dictionaryMobileForField);
		}
		return AjaxResult.success("操作成功！");
	}

	/**
	 * 删除功能字段数据字典
	 * @param infoItems
	 * @return
	 * @author DH-Xxj
	 */
	@Override
	public AjaxResult deleteDictionaryMobileForField(Map<String, Object> params) {
		List<SysDictionaryMobileForField> dictList = JSON.parseArray(params.get("infoItems").toString(), SysDictionaryMobileForField.class);
		if (dictList == null || dictList.size() == 0) {
			return AjaxResult.error("数据包格式有误，请检查！");
		}
		for (SysDictionaryMobileForField dictionaryMobileForField : dictList) {
			dictionaryMobileForFieldMapper.deleteDictionaryMobileForFieldByGuid(dictionaryMobileForField);
		}
		return AjaxResult.success("操作成功");
	}
	
}
